package io.renren.modules.generator.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.generator.entity.ContactsPeopleEntity;
import io.renren.modules.generator.service.ContactsPeopleService;
import io.renren.modules.generator.vo.ageNumVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.MissPeopleEntity;
import io.renren.modules.generator.service.MissPeopleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-01-31 16:10:18
 */
@RestController
@RequestMapping("generator/misspeople")
public class MissPeopleController {
    @Autowired
    private MissPeopleService missPeopleService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = missPeopleService.queryPage(params);
//        List<MissPeopleEntity> list = (List<MissPeopleEntity>) page.getList();
//        list.forEach(missPeople -> {
//            ContactsPeopleEntity contactsPeopleEntity = contactsPeopleService.getById(missPeople.getContactsId());
//            missPeople.setContactsPeople(contactsPeopleEntity.getContactsPeople());
//            missPeople.setContactsSex(contactsPeopleEntity.getContactsSex());
//            missPeople.setContactsPhone(contactsPeopleEntity.getContactsPhone());
//        });
//        page.setList(list);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{missId}")
    public R info(@PathVariable("missId") Integer missId){
		MissPeopleEntity missPeople = missPeopleService.getById(missId);

        return R.ok().put("missPeople", missPeople);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MissPeopleEntity missPeople){
        missPeople.setIsInner(1);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        missPeople.setCreateTime(df.format(LocalDateTime.now()));
		missPeopleService.save(missPeople);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MissPeopleEntity missPeople){
		missPeopleService.updateById(missPeople);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] missIds){
		missPeopleService.removeByIds(Arrays.asList(missIds));

        return R.ok();
    }

    /**
     * 获取年龄段占比
     */
    @RequestMapping("/missAgeNum")
    public R missAgeNum(){
        List<ageNumVo> ageNumVos = missPeopleService.missAgeNum();
        return R.ok().put("ageNum", ageNumVos);
    }


}
